package xin.cms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.cms.model.Tag;
import xin.cms.service.TagService;
import xin.common.http.request.NamePageRequest;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;

/**
 * <pre>
 * 标签管理
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/9
 */
@RestController
@RequestMapping("tag")
@Api(value = "xin-cms", description = "标签管理", position = 3)
@Slf4j
public class TagController {

    @Autowired
    private TagService service;

    @ApiOperation(value = "标签管理" , notes = "标签管理",response = PageResponse.class )
    @GetMapping("pageList")
    public PageResponse tagList(NamePageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "添加标签" , notes = "添加标签",response = Response.class )
    @PutMapping
    public Response putTag(@RequestBody Tag tag){
        return editTag(tag);
    }

    @ApiOperation(value = "编辑标签" , notes = "编辑标签",response = Response.class )
    @PostMapping
    public Response postTag(@RequestBody Tag tag){
        return editTag(tag);
    }

    @ApiOperation(value = "查看标签" , notes = "查看标签",response = Response.class )
    @GetMapping("{tagId}")
    public Response getTag(@PathVariable String tagId){
        Response<Tag> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findOne(tagId));
        return response;
    }

    private Response editTag(Tag tag){
        Response<Tag> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.save(tag));
        return response;
    }

}
